import pygame
from pygame.locals import *

class Widget():
	"""
	This is the base class for all of our UI classes.
	"""
	def __init__(self, left, top, width, height):
		"""
		Create our UI widget setting the left, top, width, height. left and top will be relative to the
		continer the widget is in, if any. 
		"""
		
		# we have two rects, one for our self, and one that is our translated screen rect
		self.rect = pygame.Rect(left, top, width, height)
		self.screenRect = pygame.Rect(left, top, width, height)

		# are we visible? used in draw() and handleEvent()
		self.visible = True

		# zOrder and parent are for when we are put into containers
		self.zOrder = 0
		self.parent = None

	def __lt__(self, other):
		"""
		We are sorted by our zOrders.
		"""
		return self.zOrder < other.zOrder

	def __gt__(self, other):
		"""
		We are sorted by our zOrders.
		"""
		return self.zOrder > other.zOrder

	def draw(self, surface):
		"""
		If we are visible, call our onDraw function.
		"""
		if self.visible:
			self.onDraw(surface)

	def isVisible(self):
		return self.visible

	def show(self):
		self.visible = True

	def hide(self):
		self.visible = False

	def move(self, pos):
		self.rect.move_ip(pos)
		self.screenRect.move_ip(pos)

	def resize(self, size):
		deltax, deltay = size

		self.rect.width += deltax
		self.screenRect.width += deltax

	def onDraw(self, surface):
		return False

	def onMouseMotion(self, event):
		return False

	def onMouseButtonDown(self, event):
		return False

	def onMouseButtonUp(self, event):
		return False

	def handleEvent(self, event):
		if self.visible:
			if event.type == MOUSEMOTION and self.screenRect.collidepoint(event.pos):
				return self.onMouseMotion(event)
			if event.type == MOUSEBUTTONUP and self.screenRect.collidepoint(event.pos):
				return self.onMouseButtonUp(event)
			if event.type == MOUSEBUTTONDOWN and self.screenRect.collidepoint(event.pos):
				return self.onMouseButtonDown(event)
			if event.type == KEYDOWN:
				return self.onKeyDown(event)
		return False

